/*     */ package com.zimbra.cs.mailbox.calendar;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZParameter;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URI;
/*     */ import java.net.URISyntaxException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.mail.internet.AddressException;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class CalendarUser
/*     */ {
/*     */   protected static final String FN_ADDRESS = "a";
/*     */   protected static final String FN_CN = "cn";
/*     */   protected static final String FN_SENTBY = "sentby";
/*     */   protected static final String FN_DIR = "dir";
/*     */   protected static final String FN_LANGUAGE = "lang";
/*     */   private String mAddress;
/*     */   private String mCn;
/*     */   private String mSentBy;
/*     */   private String mDir;
/*     */   private String mLanguage;
/*  52 */   private List<ZCalendar.ZParameter> mXParams = new ArrayList();
/*     */   
/*  54 */   public String getAddress() { return this.mAddress; }
/*  55 */   public void setAddress(String a) { this.mAddress = getMailToAddress(a); }
/*     */   
/*  57 */   public boolean hasCn() { return !StringUtil.isNullOrEmpty(this.mCn); }
/*  58 */   public String getCn() { return this.mCn; }
/*  59 */   public void setCn(String cn) { this.mCn = cn; }
/*     */   
/*  61 */   public boolean hasSentBy() { return !StringUtil.isNullOrEmpty(this.mSentBy); }
/*  62 */   public String getSentBy() { return this.mSentBy; }
/*  63 */   public void setSentBy(String sb) { this.mSentBy = getMailToAddress(sb); }
/*     */   
/*  65 */   public boolean hasDir() { return !StringUtil.isNullOrEmpty(this.mDir); }
/*  66 */   public String getDir() { return this.mDir; }
/*  67 */   public void setDir(String d) { this.mDir = d; }
/*     */   
/*  69 */   public boolean hasLanguage() { return !StringUtil.isNullOrEmpty(this.mLanguage); }
/*  70 */   public String getLanguage() { return this.mLanguage; }
/*  71 */   public void setLanguage(String lang) { this.mLanguage = lang; }
/*     */   
/*     */   private final String blankIfNullSpaceAfterIfNot(String str) {
/*  74 */     if (str == null) {
/*  75 */       return "";
/*     */     }
/*  77 */     return str + " ";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String getIndexString()
/*     */   {
/*  84 */     StringBuilder s = new StringBuilder();
/*  85 */     s.append(blankIfNullSpaceAfterIfNot(getCn()));
/*  86 */     s.append(blankIfNullSpaceAfterIfNot(getAddress()));
/*  87 */     s.append(blankIfNullSpaceAfterIfNot(getSentBy()));
/*  88 */     s.append(blankIfNullSpaceAfterIfNot(getDir()));
/*  89 */     return s.toString().trim();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public CalendarUser(String address, String cn, String sentBy, String dir, String language)
/*     */   {
/*  97 */     setAddress(address);
/*  98 */     setCn(cn);
/*  99 */     setSentBy(sentBy);
/* 100 */     setDir(dir);
/* 101 */     setLanguage(language);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public CalendarUser(String address, String cn, String sentBy, String dir, String language, List<ZCalendar.ZParameter> xparams)
/*     */   {
/* 110 */     this(address, cn, sentBy, dir, language);
/* 111 */     if (xparams != null)
/* 112 */       this.mXParams = xparams;
/*     */   }
/*     */   
/*     */   public CalendarUser(ZCalendar.ZProperty prop) {
/* 116 */     this(prop.getValue(), prop.paramVal(ZCalendar.ICalTok.CN, null), prop.paramVal(ZCalendar.ICalTok.SENT_BY, null), prop.paramVal(ZCalendar.ICalTok.DIR, null), prop.paramVal(ZCalendar.ICalTok.LANGUAGE, null));
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 122 */     for (Iterator<ZCalendar.ZParameter> paramIter = prop.parameterIterator(); paramIter.hasNext();) {
/* 123 */       ZCalendar.ZParameter param = (ZCalendar.ZParameter)paramIter.next();
/* 124 */       if (param.getToken() == null) {
/* 125 */         String name = param.getName();
/* 126 */         if ((name.startsWith("X-")) || (name.startsWith("x-")))
/* 127 */           addXParam(param);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public CalendarUser(Metadata meta) throws ServiceException {
/* 133 */     this(meta.get("a", null), meta.get("cn", null), meta.get("sentby", null), meta.get("dir", null), meta.get("lang", null));
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 139 */     List<ZCalendar.ZParameter> xparams = Util.decodeXParamsFromMetadata(meta);
/* 140 */     if (xparams != null) {
/* 141 */       for (ZCalendar.ZParameter xparam : xparams) {
/* 142 */         this.mXParams.add(xparam);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected CalendarUser(CalendarUser other) {
/* 148 */     this.mAddress = other.mAddress;
/* 149 */     this.mCn = other.mCn;
/* 150 */     this.mSentBy = other.mSentBy;
/* 151 */     this.mDir = other.mDir;
/* 152 */     this.mLanguage = other.mLanguage;
/* 153 */     this.mXParams.addAll(other.mXParams);
/*     */   }
/*     */   
/*     */   public Metadata encodeMetadata() {
/* 157 */     Metadata meta = new Metadata();
/* 158 */     meta.put("a", this.mAddress);
/* 159 */     meta.put("cn", this.mCn);
/* 160 */     meta.put("sentby", this.mSentBy);
/* 161 */     meta.put("dir", this.mDir);
/* 162 */     meta.put("lang", this.mLanguage);
/*     */     
/* 164 */     if (this.mXParams.size() > 0) {
/* 165 */       Util.encodeXParamsAsMetadata(meta, xparamsIterator());
/*     */     }
/* 167 */     return meta;
/*     */   }
/*     */   
/*     */   public InternetAddress getFriendlyAddress() throws MailServiceException
/*     */   {
/*     */     try {
/* 173 */       String address = getAddress();
/* 174 */       if ((address == null) || (address.length() < 1))
/* 175 */         throw MailServiceException.ADDRESS_PARSE_ERROR("No address value", null);
/* 176 */       InternetAddress addr; if (hasCn()) {
/* 177 */         addr = new JavaMailInternetAddress(address, getCn(), "utf-8");
/*     */       }
/* 179 */       return new JavaMailInternetAddress(address);
/*     */     }
/*     */     catch (UnsupportedEncodingException e) {
/* 182 */       throw MailServiceException.ADDRESS_PARSE_ERROR(e);
/*     */     } catch (AddressException e) {
/* 184 */       throw MailServiceException.ADDRESS_PARSE_ERROR(e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public InternetAddress getReplyAddress()
/*     */     throws MailServiceException
/*     */   {
/*     */     try
/*     */     {
/*     */       InternetAddress addr;
/*     */       
/* 197 */       if (hasSentBy()) {
/* 198 */         addr = new JavaMailInternetAddress(getSentBy());
/*     */       }
/* 200 */       return getFriendlyAddress();
/*     */     }
/*     */     catch (AddressException e)
/*     */     {
/* 204 */       throw MailServiceException.ADDRESS_PARSE_ERROR(e);
/*     */     }
/*     */   }
/*     */   
/*     */   public URI getURI() throws ServiceException {
/*     */     try {
/* 210 */       return new URI("mailto", this.mAddress, null);
/*     */     } catch (URISyntaxException e) {
/* 212 */       throw ServiceException.FAILURE("Could not create URI for address " + this.mAddress, e);
/*     */     }
/*     */   }
/*     */   
/*     */   protected abstract ZCalendar.ICalTok getPropertyName();
/*     */   
/*     */   protected void setProperty(ZCalendar.ZProperty prop) throws ServiceException {
/* 219 */     if (hasCn())
/* 220 */       prop.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.CN, getCn()));
/* 221 */     if (hasSentBy())
/* 222 */       prop.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.SENT_BY, "mailto:" + getSentBy()));
/* 223 */     if (hasDir())
/* 224 */       prop.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.DIR, getDir()));
/* 225 */     if (hasLanguage())
/* 226 */       prop.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.LANGUAGE, getLanguage()));
/*     */   }
/*     */   
/*     */   public ZCalendar.ZProperty toProperty() throws ServiceException {
/* 230 */     String addr = getAddress();
/* 231 */     if ((addr != null) && (addr.indexOf(':') < 0))
/* 232 */       addr = "mailto:" + addr;
/* 233 */     ZCalendar.ZProperty prop = new ZCalendar.ZProperty(getPropertyName(), addr);
/* 234 */     setProperty(prop);
/*     */     
/* 236 */     for (ZCalendar.ZParameter xparam : this.mXParams) {
/* 237 */       prop.addParameter(xparam);
/*     */     }
/* 239 */     return prop;
/*     */   }
/*     */   
/* 242 */   public List<ZCalendar.ZParameter> getXParams() { return this.mXParams; }
/* 243 */   public Iterator<ZCalendar.ZParameter> xparamsIterator() { return this.mXParams.iterator(); }
/*     */   
/* 245 */   public void addXParam(ZCalendar.ZParameter param) { this.mXParams.add(param); }
/*     */   
/*     */   public ZCalendar.ZParameter getXParam(String xparamName) {
/* 248 */     for (ZCalendar.ZParameter param : this.mXParams) {
/* 249 */       if (param.getName().equalsIgnoreCase(xparamName))
/* 250 */         return param;
/*     */     }
/* 252 */     return null;
/*     */   }
/*     */   
/*     */   public String toString() {
/* 256 */     StringBuilder sb = new StringBuilder();
/* 257 */     addToStringBuilder(sb);
/* 258 */     for (ZCalendar.ZParameter xparam : this.mXParams) {
/* 259 */       sb.append(", ").append(xparam.toString());
/*     */     }
/* 261 */     return sb.toString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected static String getMailToAddress(String address)
/*     */   {
/* 271 */     if (address != null) {
/* 272 */       if (address.toLowerCase().startsWith("mailto:"))
/* 273 */         address = address.substring(7);
/* 274 */       if (address.length() > 0)
/* 275 */         return address;
/*     */     }
/* 277 */     return null;
/*     */   }
/*     */   
/*     */   protected StringBuilder addToStringBuilder(StringBuilder sb) {
/* 281 */     if (hasCn()) {
/* 282 */       if (sb.length() > 0) sb.append(';');
/* 283 */       sb.append("CN=\"").append(getCn()).append('"');
/*     */     }
/* 285 */     if (hasSentBy()) {
/* 286 */       if (sb.length() > 0) sb.append(';');
/* 287 */       sb.append("SENT-BY=\"mailto:").append(getSentBy()).append('"');
/*     */     }
/* 289 */     if (hasDir()) {
/* 290 */       if (sb.length() > 0) sb.append(';');
/* 291 */       sb.append("DIR=").append(getDir());
/*     */     }
/* 293 */     if (hasLanguage()) {
/* 294 */       if (sb.length() > 0) sb.append(';');
/* 295 */       sb.append("LANGUAGE=").append(getLanguage()).append(";");
/*     */     }
/* 297 */     if (sb.length() > 0) sb.append(':');
/* 298 */     sb.append("mailto:").append(getAddress());
/* 299 */     return sb;
/*     */   }
/*     */   
/*     */   protected static <T> boolean sameValues(T val1, T val2) {
/* 303 */     if (val1 != null) {
/* 304 */       return val1.equals(val2);
/*     */     }
/* 306 */     return val2 == null;
/*     */   }
/*     */   
/*     */   public boolean equals(Object o) {
/* 310 */     if (o == this) return true;
/* 311 */     if (!(o instanceof CalendarUser)) return false;
/* 312 */     CalendarUser other = (CalendarUser)o;
/* 313 */     if (!sameValues(this.mAddress, other.mAddress))
/* 314 */       return false;
/* 315 */     if (!sameValues(this.mCn, other.mCn))
/* 316 */       return false;
/* 317 */     if (!sameValues(this.mSentBy, other.mSentBy))
/* 318 */       return false;
/* 319 */     if (!sameValues(this.mDir, other.mDir))
/* 320 */       return false;
/* 321 */     if (!sameValues(this.mLanguage, other.mLanguage))
/* 322 */       return false;
/* 323 */     if (!sameValues(this.mXParams, other.mXParams))
/* 324 */       return false;
/* 325 */     return true;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/CalendarUser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */